<?php


namespace app\lib\exception;


use think\exception\Handle;
use think\facade\Env;
use think\Response;
use Throwable;


class ExecptionHandler extends Handle
{
    public $code;
    public $msg;
    public $errorCode;

    public function render($request, Throwable $e): Response
    {
        if ($e instanceof BaseException) {
            $this->code = $e->code;
            $this->msg = $e->msg;
            $this->errorCode = $e->errorCode;
        }else{
            // debug开启，显示默认的异常
            if (Env::get('APP_DEBUG')) return parent::render($request, $e);
            //halt(Env::get('APP_DEBUG'));
            $this->code = 500;
            $this->msg = '服务器异常';
            $this->errorCode = '500';
        }

        $res = [
            'msg'=>$this->msg,
            'errorCode'=>$this->errorCode
        ];

        return json($res,$this->code);
    }


}